<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

//@TODO Añadir logs
class CTypes{
	var $ci;
	
	var $applicationTypes;
	
	var $types = array();
	var $fields = array();
	
	function CTypes(){
		ini_set('include_path',
		ini_get('include_path') . PATH_SEPARATOR . APPPATH . 'libraries');
		
		$this->ci =& get_instance();
		
		$this->ci->config->load("ctypes");
		$this->applicationTypes = $this->ci->settings->item("content_types");
		
		$this->ci->load->library('Form_validation');
		$this->ci->form_validation->set_error_delimiters('<div class="error" style="color: red">', '</div>');
	}
	
	function getTypes(){
		return $this->applicationTypes;
	}
	
	function view($name, $id=NULL){
		$view = "";
		
		$type = $this->get($name);
		
		if($type !== FALSE){
			$view = $type->view($id);			
		} else{
			echo "<br/>Contenido no existente";
		}
		
		return $view;
	}
	
	function checkCorrectSave($name){
		$correct = FALSE;
		
		$type = $this->get($name);
		
		if($type !== FALSE){
			$correct = $type->checkCorrectSave();			
		} else{
			echo "<br/>Contenido no existente";
		}
		
		return $correct;
	}
	
	//@TODO return TRUE/FALSE
	function save($name, $id=NULL){
		$type = $this->get($name);
		
		if($type !== FALSE){
			$data = $type->save($id);			
		} else{
			echo "<br/>Contenido no existente";
		}
		
		return $data;
	}
	
	//@TODO
	function delete($name, $id){
		$type = $this->get($name);
		
		$data = FALSE;
		if($type !== FALSE){
			$data = $type->delete($id);			
		}
		
		return $data;
	}
	
	function get($typeName){
		if(isset($this->types[$typeName]) && $this->types[$typeName] != NULL){
			return $this->types[$typeName];
		} else{
			return $this->_loadType($typeName);
		}
	}
	
	function _loadType($typeName){
		
		if(isset($this->types[$typeName]) && $this->types[$typeName] != NULL){
			return $this->fields[$fieldName];
		} else if(file_exists(dirname(__FILE__) . "/ctype/type/$typeName" . EXT)){
			//incluir el fichero
			include_once "ctype/type/$typeName" . EXT;
			
			//instanciar la clase y almacenarla en $this->types[$typeName]
			$class = ucwords($typeName);
			
			$obj = & new $class();
			$definition = $obj->getDefinition();
				
			foreach($definition as $typeElementName=>$typeElemenetConfig){
				$typeElement = NULL;
				
				if(isset($typeElemenetConfig['field'])){
					$typeElement = $this->getField($typeElemenetConfig['field']);
					
				} else if(isset($typeElemenetConfig['fields'])){					
					$typeElement = $this->_loadBlock($typeElementName, $typeElemenetConfig);
					
				} else if(isset($typeElemenetConfig['type'])){
					//$this->_loadType($field['type']);
					echo "<br/>Aun no se ha preparado para tipos";
					
				} else{					
					echo "<br/>Tipo no especificado correctamente";
				}
				
				
				if($typeElement != NULL && $typeElement !== FALSE){
					$typeElement = clone $typeElement;					
					$typeElement->setName($typeElementName);
					$typeElement->setFormName($typeElementName);
					
					//Cargamos las propiedades especificas del campo
					if(isset($typeElemenetConfig['properties']) && $typeElemenetConfig['properties'] != NULL){
						$typeElement->addProperties($typeElemenetConfig['properties']);
					}
					
					if(isset($typeElemenetConfig['required']) && $typeElemenetConfig['required'] === TRUE){
						$typeElement->setRequired(TRUE);
					}
					
					if(isset($typeElemenetConfig['meta']) && $typeElemenetConfig['meta'] === TRUE){
						$typeElement->setMeta(TRUE);
					}
					
					if(isset($typeElemenetConfig['meta_name']) && $typeElemenetConfig['meta_name'] != ""){
						$typeElement->setMetaName($typeElemenetConfig['meta_name']);
					}
					
					//Insertamos el typeElement en el Type
					$obj->addTypeElement($typeElement);
				} else{
					echo "<br/>Elemento no especificado correctamente '$typeElementName'";
				}
			}
			
			$this->types[$typeName] = $obj;
			
			return $obj;
		}
		
		return FALSE; 
	}
	
	//Se carga con la configuracion que le pasa el tipo
	function _loadBlock($name, $config){
		//incluir el fichero
			include_once "ctype/base/baseBlock" . EXT;
			
			//instanciar la clase y almacenarla en $this->types[$typeName]
			$class = "BaseBlock";
			
			$obj = & new $class();
			$definition = $config['fields'];
				
			foreach($definition as $typeElementName=>$typeElemenetConfig){
				$typeElement = NULL;
				
				if(isset($typeElemenetConfig['field'])){
					$typeElement = $this->getField($typeElemenetConfig['field']);
					
				} else if(isset($typeElemenetConfig['fields'])){					
					//$typeElement = $this->_loadBlock($typeElementName, $typeElemenetConfig);
					$typeElement = $this->_loadBlock($name . "_" . $typeElementName, $typeElemenetConfig);
					
				} else if(isset($typeElemenetConfig['type'])){
					//$this->_loadType($field['type']);
					echo "<br/>Aun no se ha preparado para tipos";
					
				} else{					
					echo "<br/>Tipo no especificado correctamente";
				}
				
				
				if($typeElement != NULL && $typeElement !== FALSE){
					$typeElement = clone $typeElement;					
					$typeElement->setName($typeElementName);
					$typeElement->setFormName($name . "_" . $typeElementName);
					
					//Cargamos las propiedades especificas del campo
					if(isset($typeElemenetConfig['properties']) && $typeElemenetConfig['properties'] != NULL){
						$typeElement->addProperties($typeElemenetConfig['properties']);
					}
					
					if(isset($typeElemenetConfig['required']) && $typeElemenetConfig['required'] === TRUE){
						$typeElement->setRequired(TRUE);
					}
					
					if(isset($typeElemenetConfig['meta']) && $typeElemenetConfig['meta'] === TRUE){
						$typeElement->setMeta(TRUE);
					}
					
					if(isset($typeElemenetConfig['meta_name']) && $typeElemenetConfig['meta_name'] != ""){
						$typeElement->setMetaName($typeElemenetConfig['meta_name']);
					}
					
					//Insertamos el typeElement en el Type
					$obj->addTypeElement($typeElement);
				} else{
					echo "<br/>Elemento no especificado correctamente '$typeElementName'";
				}
			}
			
			return $obj;
	}
	
	function getField($name){
		if(isset($this->fields[$name]) && $this->fields[$name] != NULL){
			return $this->fields[$name];
		} else{
			return $this->_loadField($name);
		}
	}
	
	function _loadField($fieldName){
		if(isset($this->fields[$fieldName]) && $this->fields[$fieldName] != NULL){
			return $this->fields[$fieldName];
		} else if(file_exists(dirname(__FILE__) . "/ctype/field/$fieldName" . EXT)){
			//incluir el fichero
			include_once "ctype/field/$fieldName" . EXT;
			
			//instanciar la clase y almacenarla en $this->types[$typeName]
			$class = ucwords($fieldName);
			$obj = & new $class();
						
			$this->fields[$fieldName] = $obj;
			
			return $obj;
		}
		
		return FALSE; 
	}
	
	
}
?>